package com.ksl.kinglockguard.utils;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Parcelable;

import com.ksl.kinglockguard.R;
import com.ksl.kinglockguard.activity.DetailPageShortcutActivity;
import com.ksl.kinglockguard.activity.GameListShortcutActivity;
import com.ksl.kinglockguard.activity.GamePlayActivity;
import com.ksl.kinglockguard.bitmap.ImageLoader;
import com.ksl.kinglockguard.data.Constants;
import com.ksl.kinglockguard.data.SharePreferenceSdkData;
import com.ksl.kinglockguard.data.UserCountHabit;
import com.ksl.kinglockguard.domain.H5News;

public class ShortcutUtil {
	/**
	 * 创建小游戏快捷方式
	 */
	public static void createShortCut(Context context){   
		if(SharePreferenceSdkData.isShortCut(context)){
			return;
		}
	    //创建快捷方式的Intent                     
	    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");                     
	    //不允许重复创建                     
	    shortcutintent.putExtra("duplicate", false);                     
	    //需要现实的名称                     
	    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getResources().getString(R.string.shortcut_name));     
	    //快捷图片                    
	    Parcelable icon = Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_shortcut);     
	    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);     
	    //点击快捷图片，运行的程序主入口                     
	    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(context , GameListShortcutActivity.class));                     
	    //发送广播。OK                     
	    context.sendBroadcast(shortcutintent);    
	    SharePreferenceSdkData.setIsShortCut(context, true);
	} 
	
	/**
	 * 创建小游戏快捷方式
	 */
	public static void createShortCut(Context context, H5News curNews){  
		if(curNews == null)
			return;
	    //创建快捷方式的Intent                     
	    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");                     
	    //不允许重复创建                     
	    shortcutintent.putExtra("duplicate", false);                     
	    //需要显示的名称                     
	    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, curNews.getGameName());     
	    //快捷图片       
	    File file = null;
		try {
			file = SdcardFileUtil.getFile(Constants.LOCK_SHORT_CUT, curNews.getIconUrl());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    if(file != null && file.exists()){//有则使用指定的icon
		    Bitmap deskIcon = ImageLoader.decodeFile(file,Constants.LOCK_SHORT_CUT);
		    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON, deskIcon);
	    }else{//没有则使用默认的小游戏icon
	    	Parcelable icon = Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_shortcut);
	    	shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
	    }
	    Intent intent = new Intent();
	    intent.putExtra(GamePlayActivity.EXTRA_FROM_SHORTCUT_GAMEURL_STRING, curNews.getArticleUrl());
	    intent.putExtra(GamePlayActivity.EXTRA_FROM_SHORTCUT_GAMENAME_STRING, curNews.getTitle());
	    intent.putExtra(GamePlayActivity.EXTRA_FROM_SHORTCUT_GAMEID_INT, curNews.getId());
	    intent.putExtra(GamePlayActivity.EXTRA_FROM_SHORTCUT_GAME_PLATINFOPARAM_STRING, curNews.getGame_platInfoParam());
	    
	    intent.putExtra("from", Constants.FROM_SHORT_CUT);
	    if(curNews.getEngineType() == 1){
	    	intent.setClass(context, GamePlayActivity.class);
	    }else{
	    	intent.setClass(context, DetailPageShortcutActivity.class);
	    }
	    //点击快捷图片，运行的程序主入口                     
	    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);                     
	    //发送广播。OK                     
	    context.sendBroadcast(shortcutintent);  
	    //总共生成h5游戏快捷方式数据统计
	    UserCountHabit.setUserCount(context, UserCountHabit.H5_TOTAL_ADD_SHORTCUT_NUM);
	}
}
